﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WordpressRestApi;

namespace WordpressDemo.Models
{
    public class HomeViewModel
    {
        /// <summary>Predicate:  redirect URL is (or not) available.</summary>
        public bool HasRedirectUrl
        {
            get
            {
                return RedirectURL != null;
            }
        }

        /// <summary>Client ID of the Wordpress API.</summary>
        public string ClientID { get; set; }

        /// <summary>Return URL.</summary>
        public string RedirectURL { get; private set; }

        /// <summary>URL to send the user to for authorization.</summary>
        public string UserAuthorizeUrl
        {
            get
            {
                if (HasRedirectUrl)
                {
                    var escapedRedirectUrl = HttpUtility.UrlEncode(RedirectURL);

                    return WordpressClient.GetUserAuthorizeUrl(ClientID, escapedRedirectUrl);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>Sets redirect URL from information about the request.</summary>
        /// <param name="request"></param>
        public void SetRedirectUrl(HttpRequestBase request)
        {
            var requestUrl = request.Url.ToString();
            var root = requestUrl.Replace("Home/Index", string.Empty);

            RedirectURL = root + "SignIn";
        }
    }
}